package cn.wildfire.chat.kit.conversation;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import androidx.lifecycle.ViewModelProviders;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.ArrayList;
import java.util.List;

import cn.wildfire.chat.app.extendsion.ui.SmoothToggleAndSlideAnimationForUI;
import cn.wildfire.chat.app.extendsion.ui.ToastTip;
import cn.wildfire.chat.app.main.CreateGroupNextDetaileSetActivity;
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.contact.pick.PickConversationTargetActivity;
import cn.wildfire.chat.kit.group.GroupViewModel;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.model.GroupInfo;

public class CreateConversationActivity extends PickConversationTargetActivity {

    private GroupViewModel groupViewModel;

    @Override
    protected void afterViews() {
        super.afterViews();
        groupViewModel = ViewModelProviders.of(this).get(GroupViewModel.class);
        pickConversationTargetFragment.showQuickIndexBar(true);
    }

    List<UIUserInfo> userInfos;

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        try {
            EditText searchEditText = pickConversationTargetFragment.getSearchEditText();
            if (searchEditText != null) {
                searchEditText.setFocusable(false);
                searchEditText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        searchEditText.setFocusable(true);
                        searchEditText.setFocusableInTouchMode(true);
                        searchEditText.requestFocus();
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 3);
        setReplaceTitle(null, R.string.faqi_qunliao);
        setSearchLayoutVisibility(false);
        getRightTxtView().setBackgroundResource(R.drawable.commom_green_radiusmuch);
        getRightTxtView().setTextColor(getResources().getColor(R.color.white));
        setTextThenBinClickForRightTextView(View.VISIBLE, getString(R.string.next_title), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 下一步操作
                List<UIUserInfo> initialCheckedUserInfos = pickUserViewModel.getInitialCheckedUsers();
                List<UIUserInfo> newlyCheckedUserInfos = pickUserViewModel.getCheckedUsers();
                onContactPicked(initialCheckedUserInfos, newlyCheckedUserInfos);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onContactPicked(List<UIUserInfo> initialCheckedUserInfos, List<UIUserInfo> newlyCheckedUserInfos) {
        // 单聊创建群聊时弹提示条件
        if (!initialCheckedUserInfos.isEmpty() && initialCheckedUserInfos.size() == 1 && newlyCheckedUserInfos.isEmpty()) {
            ToastTip.tip(getString(R.string.create_group_donot_less_then_2));
            return;
        }
        if (initialCheckedUserInfos.isEmpty() && newlyCheckedUserInfos.isEmpty()) {
            ToastTip.tip(getString(R.string.create_group_donot_less_then_2));
            return;
        }
        if (initialCheckedUserInfos.isEmpty() && newlyCheckedUserInfos.size() == 1) {
            Intent intent = new Intent(this, ConversationActivity.class);
            Conversation conversation = new Conversation(Conversation.ConversationType.Single, newlyCheckedUserInfos.get(0).getUserInfo().uid);
            intent.putExtra("conversation", conversation);
            startActivity(intent);
            finish();
        } else {

            userInfos = new ArrayList<>();
            userInfos.addAll(initialCheckedUserInfos);
            userInfos.addAll(newlyCheckedUserInfos);

            ArrayList<UIUserInfo> uiUserInfoArrayList = new ArrayList<>();
            uiUserInfoArrayList.addAll(userInfos);

            /**
             * modify by HEKE , 2019/8/24 3:21
             * TODO 设置群组信息窗口
             */
            pickConversationTargetFragment.showQuickIndexBar(false);
            Intent intent = new Intent(this, CreateGroupNextDetaileSetActivity.class);
            intent.putParcelableArrayListExtra("uiUserInfoArrayList", uiUserInfoArrayList);
            intent.putExtra("choosetype", CreateGroupNextDetaileSetActivity.TYPE_CREATE_GROUP);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onGroupPicked(List<GroupInfo> groupInfos) {
        Intent intent = new Intent(this, ConversationActivity.class);
        Conversation conversation = new Conversation(Conversation.ConversationType.Group, groupInfos.get(0).target);
        intent.putExtra("conversation", conversation);
        startActivity(intent);
        finish();
    }

    /**
     * 上下滑动处理
     */
    protected SmoothToggleAndSlideAnimationForUI smoothToggleAndSlideAnimationForUI;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (smoothToggleAndSlideAnimationForUI == null) {
            smoothToggleAndSlideAnimationForUI = new SmoothToggleAndSlideAnimationForUI(this, 0);
        }
        if (smoothToggleAndSlideAnimationForUI != null) {
            if (smoothToggleAndSlideAnimationForUI.isSlideViewNull()) {
                smoothToggleAndSlideAnimationForUI.setToggleView(pickConversationTargetFragment.getSearchLayout());
                smoothToggleAndSlideAnimationForUI.setSlideView(pickConversationTargetFragment.getView().findViewById(R.id.pickedUserRecyclerView), type);
            }
            smoothToggleAndSlideAnimationForUI.onTouchEvent(ev);
        }
        return super.dispatchTouchEvent(ev);
    }

}
